package com.hulk.dryad.admin.security.handler;


import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hulk.dryad.common.constant.CommonConstants;
import com.hulk.dryad.common.constant.enums.ActionType;
import com.hulk.dryad.common.constant.enums.OnlineFlag;
import com.hulk.dryad.common.util.SpringContextHolder;
import com.hulk.dryad.manage.framework.operlog.OperLogEvent;
import com.hulk.dryad.manage.framework.operlog.OperLogTypeEnum;
import com.hulk.dryad.manage.framework.operlog.OperLogUtil;
import com.hulk.dryad.manage.framework.tenant.TenantHierarchyUtil;
import com.hulk.dryad.persistence.entity.SysUserModel;
import com.hulk.dryad.admin.service.SysUserService;
import jodd.util.StringPool;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.HttpHeaders;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author hulk
 * @version 1.0
 * @date 2021/3/16 17:15
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class DryadLogoutSuccessHandler implements LogoutSuccessHandler {

	private final  ApplicationEventPublisher applicationEventPublisher;
	@Override
	public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
		long startNs = System.nanoTime();
		String referer = request.getHeader(HttpHeaders.REFERER);
		if (referer != null) {
			response.sendRedirect(referer);
		}
		User userInfo = (User)authentication.getPrincipal();
		// 删除TOKEN
		LambdaUpdateWrapper<SysUserModel> uw = Wrappers.<SysUserModel>lambdaUpdate()
				.eq(SysUserModel::getUsername, userInfo.getUsername())
				.set(SysUserModel::getOnlineFlag, OnlineFlag.N.getValue())
                .set(SysUserModel::getToken, StrUtil.uuid());
		SysUserService sysUserService = SpringContextHolder.getBean(SysUserService.class);
		sysUserService.update(uw);
		//记录退出日志
		OperLogEvent event = OperLogUtil.getOperLogDTO(OperLogTypeEnum.LOGOUT);
        event.setActionType(ActionType.OTHER.getValue());
		event.setDescription("用户退出系统成功");
		event.setClassMethod(ClassUtil.getClassName(DryadLogoutSuccessHandler.class,false)
				+ StringPool.HASH + "onLogoutSuccess");
		event.setTenantId(TenantHierarchyUtil.getTenantId());
		event.setStatus(CommonConstants.STATUS_NORMAL);
		event.setRunDuration(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs));
		applicationEventPublisher.publishEvent(event);
	}
}
